package com.peng.leetcode.other;

import java.util.ArrayList;
import java.util.List;

/**
 * Zconvert
 *
 * @author: lupeng6
 * @create: 2020/10/16 16:10
 */
public class Zconvert {

    public static void main(String[] args) {
//        System.out.println(new Solution().convert("LEETCODEISHIRING", 3));
        System.out.println(new Solution().convert("AB", 1));
    }


    static class Solution {
        public String convert(String source, int numRows) {
            if (source.length() <= 2 || numRows < 2) {
                return source;
            }

            List<StringBuilder> builders = new ArrayList<StringBuilder>();
            for (int i = 0; i < numRows; i++) {
                builders.add(new StringBuilder());
            }
            int i = 0;
            int flag = -1;
            for (char c : source.toCharArray()) {
                builders.get(i).append(c);
                if (i == 0 || i == numRows - 1) {
                    flag = -flag;
                }
                i += flag;
            }
            StringBuilder ret = new StringBuilder();
            for (StringBuilder builder : builders) {
                ret.append(builder.toString());
            }
            return ret.toString();
        }
    }
}
